﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Nova_DAL;
using System.Data.Common;
using Nova_Common;
using System.Text;

public partial class System_warehs_Warehs_List : System.Web.UI.Page
{
    DataSet ds = new DataSet();
    DbHelper dbHelper = new DbHelper();
    DbCommand cmd = null;
    string strSQL = "";
    int iFlag = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        LoginLogic.IsLogin();


        if (!IsPostBack)
        {
            gv_Bind();
        }

        //验证TextBox
        string strJs = "";
        strJs = "return Validate(";
        strJs += "'txt_WareHsCode','仓库编号不为空','r'";
        strJs += ",'txt_WareHsName','仓库名称不为空','r'";
        strJs += ");";
        ibtnSubmit.Attributes.Add("onclick", strJs);
    }

    /// <summary>
    /// 添加部门
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnSubmit_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            strSQL = "SELECT WareHsCode FROM TB_Warehs WHERE WareHsCode='" + txt_WareHsCode.Text.ToString().Trim() + "'";
            cmd = dbHelper.GetSqlStringCommond(strSQL);
            ds = dbHelper.ExecuteDataSet(cmd);

            if (ds.Tables[0].Rows.Count > 0)
            {
                ShowBox.MessageBox(this, "仓库编号已存在！");
                txt_WareHsCode.Text = "";
            }
            else
            {
                string strWareHsType = "";
                if (rbtn_WareHsType.SelectedValue == "0")
                {
                    strWareHsType = txt_WareHsCode.Text.ToString().Trim();
                }
                else
                {
                    strWareHsType = "FSSC";
                }
                strSQL = "INSERT INTO TB_Warehs VALUES('" + txt_WareHsCode.Text.ToString().Trim() + "','" + txt_WareHsName.Text.ToString().Trim() + "','" + strWareHsType.Trim() + "')";
                cmd = dbHelper.GetSqlStringCommond(strSQL);
                iFlag = dbHelper.ExecuteNonQuery(cmd);

                if (iFlag > 0)
                {
                    gv_Bind();
                    ibtnReset_Click(sender, e);//清空
                }
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }

    /// <summary>
    /// 仓库列表
    /// </summary>
    protected void gv_Bind()
    {
        strSQL = "SELECT WareHsCode,WareHsName,WareHsType=case WareHsType when 'FSSC' then '1' else '0' end FROM TB_Warehs	";
        cmd = dbHelper.GetSqlStringCommond(strSQL);
        ds = dbHelper.ExecuteDataSet(cmd);
        gvDepart.DataSource = ds;
        gvDepart.DataBind();
    }

    /// <summary>
    /// 清空
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnReset_Click(object sender, ImageClickEventArgs e)
    {
        txt_WareHsCode.Text = "";
        txt_WareHsName.Text = "";
    }

    /// <summary>
    /// 取消
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvDepart_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvDepart.EditIndex = -1;
        gv_Bind();
    }

    /// <summary>
    /// 编辑
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvDepart_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvDepart.EditIndex = e.NewEditIndex;
        gv_Bind();

    }

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvDepart_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string strID = gvDepart.DataKeys[e.RowIndex].Value.ToString();
        TextBox txtWareHsName = (TextBox)gvDepart.Rows[e.RowIndex].FindControl("txt_WareHsName");
        RadioButtonList rbtnWareHsType = (RadioButtonList)gvDepart.Rows[e.RowIndex].FindControl("rbtn_WareHsType");


        if (txtWareHsName.Text.Trim() == "")
        {
            ShowBox.MessageBox(this, "仓库名称不为空！");
            return;
        }

        try
        {
            string strWareHsType = "";
            if (rbtnWareHsType.SelectedValue == "0")
            {
                strWareHsType = strID;
            }
            else
            {
                strWareHsType = "FSSC";
            }

            strSQL = "UPDATE TB_Warehs SET WareHsName='" + txtWareHsName.Text.ToString().Trim() + "',WareHsType='" + strWareHsType + "' WHERE WareHsCode='" + strID + "'";
            cmd = dbHelper.GetSqlStringCommond(strSQL);
            iFlag = dbHelper.ExecuteNonQuery(cmd);

            if (iFlag > 0)
            {
                gvDepart.EditIndex = -1;
                gv_Bind();
                return;
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }

    ///// <summary>
    ///// 删除
    ///// </summary>
    ///// <param name="sender"></param>
    ///// <param name="e"></param>
    //protected void ibtnDelete_Click(object sender, ImageClickEventArgs e)
    //{
    //    ImageButton ibtnDelete = (ImageButton)sender;
    //    string strID = ibtnDelete.CommandArgument.Trim();

    //    if (!string.IsNullOrEmpty(ibtnDelete.CommandArgument))
    //    {
    //        try
    //        {
    //            strSQL = "SELECT * FROM ";
    //            strSQL = "DELETE TB_Warehs WHERE ID='" + Convert.ToInt32(strID) + "'";
    //            cmd = dbHelper.GetSqlStringCommond(strSQL);
    //            iFlag = dbHelper.ExecuteNonQuery(cmd);
    //        }
    //        catch (Exception ex)
    //        {
    //            Response.Write(ex.ToString());
    //        }
    //        gv_Bind();
    //    }
    //}

    protected void gvDepart_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex != -1)
        {
            RadioButtonList rbtnWareHsType = (RadioButtonList)e.Row.FindControl("rbtn_WareHsType");
            HiddenField HdWareHsType = (HiddenField)e.Row.FindControl("Hd_WareHsType");

            rbtnWareHsType.SelectedValue = HdWareHsType.Value;
        }
    }
}
